﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace childcare
{
    public partial class login : Form
    {
        private SqlConnection con;
        private DataTable dt = new DataTable("login");
        //private SqlDataAdapter da = new SqlDataAdapter();
        public login()
        {
            InitializeComponent();
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void lblpassword_Click(object sender, EventArgs e)
        {

        }

        private void btnexit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn muốn thoát ?", "THOÁT", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void btnlogin_Click(object sender, EventArgs e)
        {
            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"select * from [login] where ([user] = @username) And ([password] = @password)";
            command.Parameters.Add("@username", SqlDbType.VarChar, 30).Value = txtusername.Text;
            command.Parameters.Add("@password", SqlDbType.VarChar, 20).Value = txtpassword.Text;
            DataTable dtt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(command);
            //da.SelectCommand = command;
            da.Fill(dtt);
            if (dtt.Rows.Count > 0)
            {
                if (txtusername.Text == "Admin")
                {
                    frmadmin _frmtrangchinh = new frmadmin();
                    _frmtrangchinh.Show();
                    Hide();
                }
                else
                {
                    frmguest _frmtrangphu = new frmguest();
                    _frmtrangphu.Show();
                    Hide();
                }
            }
            else
            {
                if (MessageBox.Show("Đăng nhập thất bại bạn có muốn đăng nhập lại hay không ?", "ĐĂNG NHẬP", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    txtusername.Focus();
                }
                else
                {
                    Close();
                    System.Windows.Forms.Application.Exit();
                }
            }
        }

        private void login_Load(object sender, EventArgs e)
        {

            string connect = @"Data Source=BLACK-PC\SQLEXPRESS;Initial Catalog=Childcare;Integrated Security=True";
            con = new SqlConnection(connect);
            txtusername.Text = "Admin";
            this.AcceptButton = btnlogin;

        }
    }
}
